import {observable, action, computed, toJS, runInAction} from 'mobx';
import {Tools, Toast} from 'weaver-mobile';
import * as API from '../../apis/signlog/AppendSignApi'
import {Form} from 'weaver-mobile-page';
import objectAssign from "object-assign";
import * as SIUtil from "../../util/SignInputUtil";

const getLabel = Tools.getLabel;

export default class AppendSignLogStore {
    @observable loading = false;
    @observable form = new Form.Store();
    @observable sumitParams = {}

    controlOptions = false;//是否控制选项【主要是控制节点，控制人员】
    routepath = "";//记录路由

    condition = [];
    coreParams = {}

    doInit = (prepath = "/odocComs", params = {}, onCancel) => {
        params = {...params};
        this.coreParams = params;
        this.loading = false;
        this.routepath = prepath;
        API.odocAppendDatas(params).then(res => {
            const {api_status = false, conditions = [], controlOptions = false} = res;
            if (api_status) {
                this.condition = conditions;
                this.form = new Form.Store({route: this.routepath});
                this.form.initForm(this.condition, prepath);
                this.controlOptions = controlOptions;
                this.loading = true;
            } else {
                const {api_message = ""} = res;
            }
        });
    }


    changeSubmitParams = (datas) => {
        this.sumitParams = objectAssign({}, this.sumitParams, datas);
    }

    /**
     * 添加意见
     */
    appendPickerRemark = (v = "") => {
        const {remark=""} = this.sumitParams;
        let newremark = `${remark}${v}`;
        this.changeSubmitParams({remark: newremark})
    }

    /**
     * 保存数据
     * @param workflowStores
     * @param onCancel
     */
    doSave = (workflowStores = {}, onCancel) => {
        let pass = this.form.doValidate();
        if (pass.pass) {
            const {globalStore = {}} = workflowStores;
            const {submitParam = {}} = globalStore;
            // let params = toJS(objectAssign({}, this.form.formParams, this.coreParams, submitParam, this.sumitParams));
            let params = toJS(objectAssign({}, this.form.formParams, this.coreParams, this.sumitParams));
            if(!SIUtil.checkRemark(params)) {
                Toast.info(getLabel(500238, "请输入签字意见！"), 1);
                return;
            }
            this.loading = false;
            API.saveSignSetting(params).then(res => {
                const {api_status = false, api_message = ""} = res;
                if (api_status) {
                    Toast.success(api_message || getLabel(83551, "保存成功！"), 2, () => {
                        this.loading = true;
                        onCancel(res)
                    });
                } else {
                    this.loading = true;
                    Toast.fail(api_message)
                }
            })
        }
    }

    /**
     * 通过操作者限制操作者节点
     * @param params
     */
    getSqlWhere = (params = {}) => {
        params = {...params, ...this.coreParams};
        this.controlOptions && API.getSqlWhere(params).then(res => {
            const {nodes, api_status = false} = res;
            this.fnChangeForm({nodes: nodes}, "appendToNodeid");
        })
    }

    /**
     *
     * @param params
     * @param key
     */
    fnChangeForm = (params = {}, key = "") => {
        let {condition, routepath} = this;
        condition.length > 0 && condition.map(group => {
            group.items = group.items.map(item => {
                const {domkey = []} = item;
                if (domkey.indexOf(key) != -1) {
                    let {browserConditionParam = {}} = item;
                    let {completeParams = {}, conditionDataParams = {}, dataParams = {}} = browserConditionParam;
                    completeParams = {...completeParams, ...params};
                    conditionDataParams = {...conditionDataParams, ...params};
                    dataParams = {...dataParams, ...params};
                    browserConditionParam = {...browserConditionParam, completeParams, conditionDataParams, dataParams};
                    item = {...item, browserConditionParam}
                }
                return item;
            });
            return group;
        });
        runInAction(() => {
            let datas = this.form.formData;
            datas && datas[key] && (delete datas[key]);//删除被联动值
            this.condition = condition;
            this.form = new Form.Store({route: routepath});
            this.form.initForm(condition, routepath);
            this.form.updateFields(datas)
        })
    }

    destory = () => {
        this.form.resetFields();
        this.sumitParams = {};
        // this.coreParams = {};
    }

}
